import java.util.*;

/**
 * @author LKQ
 * @date 2022/5/1 21:11
 * @description HashMap实现双映射
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] words = {"bbc", "cde"};
        solution.findAndReplacePattern(words, "abb");
    }
    public List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> ans = new ArrayList<>();
        for (String word: words) {
            if (match(word, pattern)) {
                ans.add(word);
            }
        }
        return ans;
    }

    public boolean match(String word, String pattern) {
        Map<Character, Character> m1 = new HashMap<>();
        Map<Character, Character> m2 = new HashMap<>();

        for (int i = 0; i < word.length(); ++i) {
            char w = word.charAt(i);
            char p = pattern.charAt(i);
            if (!m1.containsKey(w)) m1.put(w, p);
            if (!m2.containsKey(p)) m2.put(p, w);
            // 如果存储的 a->b，这一次为a，但不是b，那么映射错误
            if (m1.get(w) != p || m2.get(p) != w)
                return false;
        }

        return true;
    }
}
